package com.larry.isn;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import com.larry.isn.service.IsnService;
import com.larry.util.LarryConst;
import com.larry.util.LarryUtil;
import com.pegatron.ers.server.app.helper.ContextHelper;
import com.pegatron.ers.vo.common.CommonDataModel;
import com.pegatron.ers.vo.common.CommonSQLStmt;
import com.pegatron.ers.vo.common.MultiResultModel;

public class DataSearcher {

	private String startDate = "2013/01/01";

	private Integer dateRange = 30;

	private String endDate;

	public DataSearcher() {
		try {
			endDate = this.getNextDate(startDate, dateRange - 1);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public List<String> getDatas2() throws Exception {
		LarryFileReader reader = new LarryFileReader(new File("D:/Workspace-3/IsnSearcher/serial.txt"));
		return reader.read();
	}
	
	public List<String> getDatas() throws Exception {
		List<String> list = this.getDataFromFile();
		while (list.size() < 10000) {
			list = this.addDataFromDB();
		}
		return list;
	}

	private List<String> getDataFromFile() throws Exception {
		LarryFileReader reader = new LarryFileReader(LarryUtil.getSrcFile(LarryConst.DATA_FILE));
		return reader.read();
	}

	private List<String> addDataFromDB() throws Exception {
		List<String> list = this.getDataFromErs();
		this.updateDataFile(list);
		this.updateConfig();
		return this.getDataFromFile();
	}

	private List<String> getDataFromErs() throws Exception {
		List<String> list = new ArrayList<String>();
		Properties prop = new Properties();
		prop.load(new FileInputStream(LarryConst.CONFIG_PROPERTIES));
		String table = prop.getProperty(LarryConst.SEARCH_TABLE);
		if (!"".equals(prop.getProperty(LarryConst.SEARCH_START_DATE))) {
			startDate = this.getNextDate(prop.getProperty(LarryConst.SEARCH_START_DATE), dateRange);
		}
		if (!"".equals(prop.getProperty(LarryConst.SEARCH_END_DATE))) {
			endDate = this.getNextDate(prop.getProperty(LarryConst.SEARCH_END_DATE), dateRange);
		}
		CommonSQLStmt stmt = IsnService.getSql(table, startDate, endDate);
		MultiResultModel result = ContextHelper.getSQLStatementService().findAll(stmt);
		List<CommonDataModel> datas = result.getDatas();
		for (CommonDataModel data : datas) {
			list.add(data.getString("serial_nr"));
		}
		return list;
	}

	private void updateDataFile(List<String> list) throws Exception {
		Map<String, String> map = new HashMap<String, String>();
		List<String> fileDatas = this.getDataFromFile();
		for (String fileData : fileDatas) {
			map.put(fileData, fileData);
		}
		for (String data : list) {
			map.put(data, data);
		}
		list = new ArrayList<String>(map.values());
		LarryFileWriter writer = new LarryFileWriter(LarryUtil.getSrcFile(LarryConst.DATA_FILE));
		writer.write(list);
	}

	private void updateConfig() throws Exception {
		Properties prop = new Properties();
		prop.load(new FileInputStream(LarryConst.CONFIG_PROPERTIES));
		prop.setProperty(LarryConst.SEARCH_START_DATE, startDate);
		prop.setProperty(LarryConst.SEARCH_END_DATE, endDate);
		prop.store(new FileOutputStream(LarryConst.CONFIG_PROPERTIES), null);
	}

	private String getNextDate(String date, Integer dateRange) throws Exception {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
		Calendar cal = Calendar.getInstance();
		cal.setTime(sdf.parse(date));
		cal.add(Calendar.DATE, dateRange);
		return sdf.format(cal.getTime());
	}
}
